package java3_31;

import java.io.*;

/**
 *4:获得文件行数的方法
 *有 bufferedReader.readLine 方法 或者查找byte是否=='\n',比较谁快！
 */
public class Homework4 {
    public static void main(String[] args) {
        long start = System.nanoTime();
        try {
            FileInputStream fis = new FileInputStream("G:\\JAVA84\\chaoxian1.txt");
            BufferedReader br = new BufferedReader(new FileReader("G:\\JAVA84\\chaoxian1.txt"));

            int count1 = 0;
            int count2 = 0;
            while (br.readLine()!=null){
                count1++;
            }
            br.close();
            System.out.println(count1);
            long end = System.nanoTime();
            System.out.println("获得文件行数的时间是："+(end - start));

            int num = 0;
            long start2 = System.nanoTime();
            while ((num = fis.read()) != -1) {
                if (num == '\n') {
                    count2++;
                }
            }
            long end2 = System.nanoTime();
            System.out.println("行数：" + (count2+1));//最后一行读不到'\n'
            System.out.println("查找byte的时间：" + (end2 - start2));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
